package com.example.ffepeg.ffeAdder;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class WatermarkAdder {
    public static boolean replaceLogo(String inputFile, String outputFile, int x, int y, String logoFile) {
        try {
            // 构建FFmpeg命令
            ProcessBuilder processBuilder = new ProcessBuilder(
                    "ffmpeg", "-i", inputFile, "-i", logoFile,
                    "-filter_complex", "overlay=" + x + ":" + y, "-c:a", "copy", outputFile
            );
            processBuilder.redirectErrorStream(true);

            // 启动进程
            Process process = processBuilder.start();

            // 读取标准输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.err.println(line);
            }

            // 等待进程完成
            int exitCode = process.waitFor();
            return exitCode == 0; // 如果退出码为0，则表示成功
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    public static void main(String[] args) {
        //String inputVideo = "https://aigc-files.bigmodel.cn/api/cogvideo/0171cd58-faf8-11ef-ad3e-d2a032528fb8_0.mp4";
        //String inputVideo = "https://aigc-files.bigmodel.cn/api/cogvideo/557b85e2-fafd-11ef-96f4-767badf28077_0.mp4";
        String inputVideo = "https://pushimg.liecaizhijia.com/ai_conflate/video2025-03-07/1813b267-e878-46c3-957a-14a3a3dd7ae4.mp4";
        String outputVideo = "D:\\javaXM\\FFEpeg\\src\\main\\resources\\out\\video_with_watermark.mp4";
      //  String watermarkImage = "https://pushimg.liecaizhijia.com/image_example/img/c6dd68e8-31da-4af8-b255-14082a279fff51826714613360d6ca65957966e675a.png";
        String logoImagePath = "https://pushimg.liecaizhijia.com/image_example/img/c6dd68e8-31da-4af8-b255-14082a279fff51826714613360d6ca65957966e675a.png"; // 水印图片路径透明底色
        int x = 0; // 水印的x坐标
        int y = 0; // 水印的y坐标
        boolean  boo = replaceLogo(inputVideo, outputVideo, x, y, logoImagePath);
        System.out.println("================: " + boo);
    }

}
